import org.apache.commons.logging.LogFactory;
import pojo.*;
import service.*;
import org.apache.commons.logging.Log;
import utils.SystemEnum;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class MainSystem {
    private AdminService adminService;
    private BedService bedService;
    private ChangeRoomService changeRoomService;
    private CheckInRecordService checkInRecordService;
    private CheckOutRecordService checkOutRecordService;
    private ClassroomService classroomService;
    private PayRecordService payRecordService;
    private RoomService roomService;
    private StudentService studentService;
    private Scanner scanner;

    //日志
    Log log;

    private int permission = -1;        // -1未初始化 0游客 1管理员  2超级管理员
    private String allMenu= "0.登录\n1. 房间管理\n" +
            "2. 住宿管理\n" +
            "3. 班级管理\n" +
            "4. 信息查询\n" +
            "5. 数据统计\n" +
            "6. 费用管理\n" +
            "7. 系统管理\n" +
            "8. 退出";
    private String superAdmin ="0. 退出\n" +
            "1. 房间管理：\n" +
            "\t11. 房间录入:\n" +
            "\t12. 房间修改：\n" +
            "2. 班级管理：\n" +
            "\t21.添加班级\n" +
            "\t22.修改班级\n" +
            "3.系统管理：\n" +
            "\t31.添加登陆用户\n" +
            "\t32.人员删除\n" +
            "\t33.人员信息修改\n" +
            "\t34.删除房间\n" +
            "\t35.删除学员";

    private String adminMenu ="0. 退出\n" +
            "1. 住宿管理：\n" +
            "\t11.办理入住：\n" +
            "\t12.调换房间：\n" +
            "\t13.办理退宿：\n" +
            "\t14.入住修改：\n" +
            "2. 信息查询：\n" +
            "\t21.住宿查询：\n" +
            "\t\t211.根据房间号查询\n" +
            "\t\t212.根据房间入住性别查询\n" +
            "\t22.个人信息查询\n" +
            "\t\t221.根据学员编号\n" +
            "\t\t222.根据姓名查询\n" +
            "\t23.房间查询\n" +
            "\t\t231根据房间号查询\n" +
            "\t24.班级查询\n" +
            "\t\t241.根据班级编号\n" +
            "\t\t242.根据班级名称\n" +
            "\t25.性别查询学生\n" +
            "\t\t251.根据性别查询学员\n" +
            "\t26.欠费查询\n" +
            "\t27.钥匙领取查询\n" +
            "\t\t271.查询已领取学生\n" +
            "\t\t272.查询未领取学生\t\n" +
            "\t28.记录查询\n" +
            "\t\t281.调房记录查询\n" +
            "\t\t282.缴费记录查询\n" +
            "\t\t283.退宿记录查询\n" +
            "\t\n" +
            "3.数据统计：\n" +
            "\t31.人数统计-按时间段统计：\n" +
            "\t32.房间信息统计：\n" +
            "\t\t321.统计当前入住房间的总人数\n" +
            "\t\t322.房间数\n" +
            "\t\t323.房间的可住人数总计\n" +
            "4.费用管理：\n" +
            "\t41.费用缴纳：\n" +
            "\t42.费用查询：\n" +
            "\t\t421.已缴费用\n" +
            "\t\t422.未缴费用\n" +
            "\t\t423.费用差 \n" +
            "\t43.费用统计：\n" +
            "\t\t431.应缴费用\n" +
            "\t\t432.实缴费用\n" +
            "\t\t433.未缴费用\n" +
            "\t\t434.押金\n";
    public MainSystem() {
        //初始化log
        log = LogFactory.getLog(MainSystem.class);
        log.info(" MainSystem()-系统开始初始化");
        
        adminService = new AdminServiceImpl();
        //new
        bedService = new BedServiceImpl();
        changeRoomService = new ChangeRoomServiceImpl();
        checkInRecordService = new CheckInRecordServiceImpl();
        checkOutRecordService = new CheckOutRecordServiceImpl();
        classroomService = new ClassroomServiceImpl();
        payRecordService = new PayRecordServiceImpl();
        roomService = new RoomServiceImpl();
        studentService = new StudentServiceImpl();
        permission = 0;
        scanner = new Scanner(System.in);
    }

    public void menu(){
        String menu = null;
        String status = permission==0?"游客":(permission==1?"管理员":"超级管理员");
        switch (permission){
            case 0: menu = allMenu;
                break;
            case 1:
                menu = adminMenu;
                break;
            case 2:
                menu = superAdmin;
                break;
            default:
                return;
        }
        System.out.println("您当前身份是："+status+"\n"+menu);
        System.out.println("请输入：");
    }
/////////////////////////////////////////////////////////////////     程序入口
    public static void main(String[] args) {
        MainSystem mainSystem = new MainSystem();
//        mainSystem.start();
        mainSystem.findActualPayment();
    }
////////////////////////////////////////////////////////////////

    public void start(){
        log.info(" MainSystem()-start()");
        System.out.println("欢迎使用宿舍管理系统");
        while(permission!=-1){
            switch (permission){
                case 0:
                    anonymity();
                break;
                case 1:
                    admin();
                    break;
                case 2:
                    superAdmin();
                    break;
            }
        }
    }

    //退出
    public void end(){
        log.info(" MainSystem()-end()");
        System.out.println("欢迎使用宿舍管理系统");
        System.out.println("正在关机");
        permission = -1;
        scanner.close();
    }

    //登录
    public void login(){
        log.info(" MainSystem()-login()");
        try{
            System.out.println("输入姓名：");
            Integer username = scanner.nextInt();
            System.out.println("输入密码：");
            String password = scanner.next();
            if(adminService.checkAdminPassword(username,password)) {
                System.out.println("登录成功");
                if(adminService.findByAdminAcount(username).isSuper()) permission = 2;
                else permission = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //游客
    public void anonymity(){
        menu();
        int chose = -1;
        try {
            chose = scanner.nextInt();
            switch (chose){
                case 0:
                    login();
                    break;
                case 8:
                    end();
                    return;
                default:
                    System.out.println("您无权限查看！");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //普通管理员
    public void admin(){
        menu();
        int chose = -1;
        try {
            chose = scanner.nextInt();
            switch (chose){
                default:
                    System.out.println("非法输入！");
                case 0:
                    System.out.println("返回上一级");
                    permission-=1;
                    break;
                case 11:
                    System.out.println("住宿管理-办理入住");
                    check_in();
                    break;
                case 12:
                    System.out.println("住宿管理-调换房间");
                    change_room();
                    break;
                case 13:
                    System.out.println("住宿管理-办理退宿");
                    check_out();
                    break;
                case 14:
                    System.out.println("住宿管理-入住修改");
                    alterRoom();
                    break;
                case 211:
                    System.out.println("信息查询-住宿查询-根据房间号查询");
                    findByRoomId();
                    break;
                case 212:
                    System.out.println("信息查询-住宿查询-根据房间入住性别查询");
                    findByRoomSex();
                    break;
                case 221:
                    System.out.println("信息查询-个人信息查询-根据学员编号");
                    findByStuId();
                    break;
                case 222:
                    System.out.println("信息查询-个人信息查询-根据姓名查询");
                    findByStuName();
                    break;
                case 231:
                    System.out.println("信息查询-房间查询-根据房间号查询");
                    findByRoomId();
                    break;
                case 241:
                    System.out.println("信息查询-班级查询-根据班级编号");
                    findByClassId();
                    break;
                case 242:
                    System.out.println("信息查询-班级查询-根据班级名称");
                    findByClassName();
                    break;
                case 251:
                    System.out.println("信息查询-性别查询学生-根据性别查询学员");
                    findByStuSex();
                    break;
                case 26:
                    System.out.println("信息查询-欠费查询");
                    findArrearage();
                    break;
                case 271:
                    System.out.println("信息查询-钥匙领取查询-查询已领取学生");
                    findIsTaken();
                    break;
                case 272:
                    System.out.println("信息查询-钥匙领取查询-查询未领取学生");
                    findNoTaken();
                    break;
                case 281:
                    System.out.println("信息查询-记录查询-调房记录查询");
                    findChangeRecord();
                    break;
                case 282:
                    System.out.println("信息查询-记录查询-缴费记录查询");
                    findPayRecord();
                    break;
                case 283:
                    System.out.println("信息查询-记录查询-退宿记录查询");
                    findCheckOutRecord();
                    break;
                case 31:
                    System.out.println("数据统计-人数统计-按时间段统计人数");
                    findCheckInRecord();
                    break;
                case 321:
                    System.out.println("数据统计-房间信息统计-统计当前入住房间的总人数");
                    findPeoTotal();
                    break;
                case 322:
                    System.out.println("数据统计-房间信息统计-房间数");
                    findRoomTotal();
                    break;
                case 323:
                    System.out.println("数据统计-房间信息统计-房间的可住人数总计");
                    findCanLiveTotal();
                    break;
                case 41:
                    System.out.println("费用管理-费用缴纳");
                    pay();
                    break;
                case 421:
                    System.out.println("费用管理-费用查询-已缴费用");
                    findActualPayment();
                    break;
                case 422:
                    System.out.println("费用管理-费用查询-未缴费用");
                    findNoPay();
                    break;
                case 431:
                    System.out.println("费用管理-费用统计-应缴费用");
                    findPaymentInTime();
                    break;
                case 432:
                    System.out.println("费用管理-费用统计-实缴费用");
                    findActualPaymentInTime();
                    break;
                case 433:
                    System.out.println("费用管理-费用统计-未缴费用");
                    findNoPayInTime();
                    break;
                case 434:
                    System.out.println("费用管理-费用统计-押金");
                    findCashInTime();
                    break;

            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //11. 办理入住
    private void check_in() throws ParseException {
        System.out.println(studentService.findAll());
        System.out.println("输入学生学号");
        Integer id = scanner.nextInt();
        System.out.println("输入房间Id");
        Integer roomId = scanner.nextInt();
        System.out.println("输入床id");
        Integer bedId = scanner.nextInt();
        System.out.println("输入预计离开日期");
        String date = scanner.next();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date out_date = null;
        try {
            out_date = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println("输入缴费金额:");
        BigDecimal actualBigDecimal = scanner.nextBigDecimal();
        System.out.println("是否拿钥匙？");
        boolean tag = scanner.nextBoolean();
        System.out.println("输入缴费押金：");
        BigDecimal cashBigDecimal = scanner.nextBigDecimal();

        CheckInRecord checkInRecord = new CheckInRecord(0,id,roomId,bedId,new Date(),out_date,new BigDecimal(1400),actualBigDecimal,tag,cashBigDecimal);
        checkInRecordService.insert(checkInRecord);

    }

    //12. 调换房间
    private void change_room(){
        System.out.println(checkInRecordService.findAll());
        System.out.println("输入要调换入住信息的id");
        Integer id = scanner.nextInt();
        System.out.println("调换的新房间号：");
        Integer roomId = scanner.nextInt();
        System.out.println("调换的床号：");
        Integer bedId = scanner.nextInt();

        CheckInRecord checkInRecord = checkInRecordService.findById(id);
        ChangeRoom changeRoom = new ChangeRoom(0,checkInRecord.getStuId(),checkInRecord.getDormId(),checkInRecord.getBedId(),roomId,bedId,new Date(),checkInRecord.getCheck_in_id());
        changeRoomService.update(changeRoom);
    }

    //13.办理退宿
    private void check_out(){
        System.out.println(checkInRecordService.findAll());
        System.out.println("输入入住信息ID");
        Integer id = scanner.nextInt();
        CheckInRecord checkInRecord = checkInRecordService.findById(id);
        System.out.println("输入退宿原因");
        String reson = scanner.next();
        CheckOutRecord checkOutRecord = new CheckOutRecord(checkInRecord.getStuId(),checkInRecord.getDormId(),checkInRecord.getBedId(),new Date(),reson);
        checkOutRecordService.insert(checkOutRecord);
    }
    //14. 入住修改
    private void alterCheck_in(){
        System.out.println(checkInRecordService.findAll());
        System.out.println("输入要修改的id");
        Integer id = scanner.nextInt();
        System.out.println("输入要修改的字段名");
        String field = scanner.next();
        System.out.println("输入要修改的字段值");
        String fieldValue = scanner.next();
        CheckInRecord checkInRecord = checkInRecordService.findById(id);
        switch (field){
            case "stuId":
                checkInRecord.setStuId(Integer.parseInt(fieldValue));
                break;
            case "check_in_time":
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    checkInRecord.setCherk_in_time(simpleDateFormat.parse(fieldValue));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            case "plan_check_out_time":
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    checkInRecord.setPlan_cherk_out_time(simpleDateFormat2.parse(fieldValue));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            case "isTakenKey":
                checkInRecord.setTakenKey(!checkInRecord.isTakenKey());
                break;
            default:
                return;
        }

        checkInRecordService.update(checkInRecord);
    }
    //211.231.信息查询-住宿查询-根据房间号查询
    private void findByRoomId() {
        try{
            System.out.println("输入查询房间号：");
            Integer roomId = scanner.nextInt();
            ArrayList<CheckInRecord> checkInRecords = checkInRecordService.findByRoomId(roomId);
            Room room = roomService.findById(roomId);
            System.out.println(room);
            if(checkInRecords.size()!=0){
                System.out.println(checkInRecords);
                for (CheckInRecord checkInRecord : checkInRecords) {
                    Student student = studentService.findByStuId(checkInRecord.getStuId());
                    System.out.println(student);
                }
            }else {
                System.out.println("该房间暂不存在入住信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //212.信息查询-住宿查询-根据房间入住性别查询
    private void findByRoomSex() {
        try{
            System.out.println("输入查询性别：");
            String sex = scanner.next();
            ArrayList<Room> rooms = roomService.findBySex(sex);
            if(rooms.size()!=0){
                for (Room room : rooms) {
                    System.out.println(room);
                    ArrayList<CheckInRecord> checkInRecords = checkInRecordService.findByRoomId(room.getRoomId());
                    if(checkInRecords!=null){
                        System.out.println(checkInRecords);
                        for (CheckInRecord checkInRecord : checkInRecords) {
                            Student student = studentService.findByStuId(checkInRecord.getStuId());
                            System.out.println(student);
                        }
                    }else {
                        System.out.println("该房间暂不存在入住信息");
                    }
                }
            }else {
                System.out.println("不存在该性别房间");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //221.信息查询-个人信息查询-根据学员编号
    private void findByStuId() {
        try{
            System.out.println("输入查询学生编号：");
            Integer stuId = scanner.nextInt();
            Student student = studentService.findByStuId(stuId);
            if(student!=null){
                System.out.println(student);
                CheckInRecord checkInRecord = checkInRecordService.findByStuId(stuId);
                if(checkInRecord!=null){
                    System.out.println(checkInRecord);
                }else {
                    System.out.println("该用户暂不存在入住信息");
                }
            }else {
                System.out.println("用户不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //222.信息查询-个人信息查询-根据姓名查询
    private void findByStuName() {
        try{
            System.out.println("输入查询学生姓名：");
            String stuName = scanner.next();
            Student student = studentService.findByStuName(stuName);
            if(student!=null){
                System.out.println(student);
                CheckInRecord checkInRecord = checkInRecordService.findByStuName(stuName);
                if(checkInRecord!=null){
                    System.out.println(checkInRecord);
                }else {
                    System.out.println("该用户暂不存在入住信息");
                }
            }else {
                System.out.println("用户不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //241.信息查询-班级查询-根据班级编号
    private void findByClassId() {
        try{
            System.out.println("输入查询班级编号：");
            Integer classId = scanner.nextInt();
            Classroom classroom = classroomService.findById(classId);
            if(classroom!=null){
                System.out.println(classroom);
                ArrayList<Student> list = studentService.findByClassId(classId);
                for (Student student : list) {
                    System.out.println(student);
                    CheckInRecord checkInRecord = checkInRecordService.findByStuId(student.getStuId());
                    if(checkInRecord!=null){
                        System.out.println(checkInRecord);
                    }else {
                        System.out.println("该用户暂不存在入住信息");
                    }
                }
            }else {
                System.out.println("班级不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //242.信息查询-班级查询-根据班级名称
    private void findByClassName() {
        try{
            System.out.println("输入查询班级名称：");
            String ClassName = scanner.next();
            Classroom classroom = classroomService.findByName(ClassName);
            if(classroom!=null){
                System.out.println(classroom);
                ArrayList<Student> list = studentService.findByClassId(classroom.getClassId());
                for (Student student : list) {
                    System.out.println(student);
                    CheckInRecord checkInRecord = checkInRecordService.findByStuId(student.getStuId());
                    if(checkInRecord!=null){
                        System.out.println(checkInRecord);
                    }else {
                        System.out.println("该用户暂不存在入住信息");
                    }
                }
            }else {
                System.out.println("班级不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //251.信息查询-性别查询学生-根据性别查询学员
    private void findByStuSex() {
        try{
            System.out.println("输入查询学生性别：");
            String stuSex = scanner.next();
            ArrayList<Student> list = studentService.findByStuSex(stuSex);
            if(list.size()!=0){
                for (Student student : list) {
                    System.out.println(student);
                    CheckInRecord checkInRecord = checkInRecordService.findByStuId(student.getStuId());
                    if(checkInRecord!=null){
                        System.out.println(checkInRecord);
                    }else {
                        System.out.println("该用户暂不存在入住信息");
                    }
                }
            }else {
                System.out.println("用户不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //26.信息查询-欠费查询
    private void findArrearage() {
        try{
            ArrayList<CheckInRecord> checkInRecords = checkInRecordService.findArrearage();
            if(checkInRecords.size()!=0){
                for (CheckInRecord checkInRecord : checkInRecords) {
                    Student student = studentService.findByStuId(checkInRecord.getStuId());
                    System.out.println(student);
                    System.out.println(checkInRecord);
                }
            }else {
                System.out.println("未存在欠费信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //271.信息查询-钥匙领取查询-查询已领取学生
    private void findIsTaken() {
        try{
            ArrayList<CheckInRecord> checkInRecords = checkInRecordService.findIsTaken();
            if(checkInRecords.size()!=0){
                for (CheckInRecord checkInRecord : checkInRecords) {
                    Student student = studentService.findByStuId(checkInRecord.getStuId());
                    System.out.println(student);
                    System.out.println(checkInRecord);
                }
            }else {
                System.out.println("全部未领取");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //272.信息查询-钥匙领取查询-查询未领取学生
    private void findNoTaken() {
        try{
            ArrayList<CheckInRecord> checkInRecords = checkInRecordService.findNoTaken();
            if(checkInRecords.size()!=0){
                for (CheckInRecord checkInRecord : checkInRecords) {
                    Student student = studentService.findByStuId(checkInRecord.getStuId());
                    System.out.println(student);
                    System.out.println(checkInRecord);
                }
            }else {
                System.out.println("全部已领取");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //281.信息查询-记录查询-调房记录查询
    private void findChangeRecord() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            ArrayList<ChangeRoom> changeRooms = changeRoomService.findDate(begin, end);
            if(changeRooms.size()!=0){
                for (ChangeRoom changeRoom : changeRooms) {
                    Student student = studentService.findByStuId(changeRoom.getStuId());
                    System.out.println(student);
                    System.out.println(changeRoom);
                }
            }else {
                System.out.println("该时间段内没有记录");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //282.信息查询-记录查询-缴费记录查询
    private void findPayRecord() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            ArrayList<PayRecord> payRecords = payRecordService.findDate(begin, end);
            if(payRecords.size()!=0){
                for (PayRecord payRecord : payRecords) {
                    Student student = studentService.findByStuId(payRecord.getStuId());
                    System.out.println(student);
                    System.out.println(payRecord);
                }
            }else {
                System.out.println("该时间段内没有记录");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //283.信息查询-记录查询-退宿记录查询
    private void findCheckOutRecord() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            ArrayList<CheckOutRecord> checkOutRecords = checkOutRecordService.findDate(begin, end);
            if(checkOutRecords.size()!=0){
                for (CheckOutRecord checkOutRecord : checkOutRecords) {
                    Student student = studentService.findByStuId(checkOutRecord.getStuId());
                    System.out.println(student);
                    System.out.println(checkOutRecord);
                }
            }else {
                System.out.println("该时间段内没有记录");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //31.数据统计-人数统计-按时间段统计人数
    private void findCheckInRecord() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            System.out.println("当前时间段的人数："+checkInRecordService.findDate(begin,end));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //321.数据统计-房间信息统计-统计当前入住房间的总人数
    private void findPeoTotal() {
        try{
            System.out.println("当前入住房间的总人数："+checkInRecordService.findTotal());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //322.数据统计-房间信息统计-房间数
    private void findRoomTotal() {
        try{
            System.out.println("房间数："+roomService.findRoomTotal());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //323.数据统计-房间信息统计-房间的可住人数总计
    private void findCanLiveTotal() {
        try{
            System.out.println("房间的可住人数总计："+bedService.findCanLiveTotal());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //421.费用管理-费用查询-已缴费用
    private void findActualPayment() {
        try{
            System.out.println("已缴费用总数为："+checkInRecordService.findActualPayment());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //422.费用管理-费用查询-未缴费用
    private void findNoPay() {
        try{
            System.out.println("未缴费用总数为："+checkInRecordService.findNoPay());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //431.费用管理-费用统计-应缴费用
    private void findPaymentInTime() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            System.out.println("应缴费用总数为："+checkInRecordService.findPaymentInTime(begin,end));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //41.费用交纳
    private void pay(){
        System.out.println("输入要交纳的入住记录id");
        Integer integer = scanner.nextInt();

        System.out.println("输入要交纳的金额");
        BigDecimal bigDecimal = scanner.nextBigDecimal();
        System.out.println("输入交纳的项目：true-住宿费 false-押金");

        //获取入住记录中学生id
        CheckInRecord checkInRecord = checkInRecordService.findById(integer);
        boolean tag = scanner.nextBoolean();
        PayRecord payRecord = new PayRecord(0,checkInRecord.getStuId(),new Date(),bigDecimal,tag);
        //交纳
        payRecordService.insert(payRecord);
    }

    //432.费用管理-费用统计-实缴费用
    private void findActualPaymentInTime() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            System.out.println("实缴费用总数为："+checkInRecordService.findActualPaymentInTime(begin,end));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //433.费用管理-费用统计-未缴费用
    private void findNoPayInTime() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            System.out.println("未交费用总数为："+checkInRecordService.findNoPayInTime(begin,end));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //434.费用管理-费用统计-押金
    private void findCashInTime() {
        try{
            System.out.println("输入起始时间：");
            String date1 = scanner.next();
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = stf.parse(date1);
            System.out.println("输入结束时间：");
            String date2 = scanner.next();
            Date end = stf.parse(date2);
            System.out.println("押金总数为："+checkInRecordService.findCashInTime(begin,end));
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //超级管理员
    public void superAdmin(){
        menu();
        int chose = -1;
        try {
            chose = scanner.nextInt();
            switch (chose){
                default:
                    System.out.println("输入错误！");
                case 0:
                    System.out.println("返回上一级");
                    permission -=1;
                    break;
                case 11:
                    System.out.println("房间管理-房间录入");
                    addARoom();
                    break;
                case 12:
                    System.out.println("房间管理-房间修改");
                    alterRoom();
                    break;
                case 21:
                    System.out.println("班级管理-添加班级");
                    addClass();
                    break;
                case 22:
                    System.out.println("班级管理-修改班级");
                    alterClass();
                    break;
                case 31:
                    System.out.println("系统管理-添加登陆用户");
                    addAdmin();
                    break;
                case 32:
                    System.out.println("系统管理-人员删除");
                    deleteAdmin();
                    break;
                case 33:
                    System.out.println("系统管理-人员信息修改");
                    alterAdmin();
                    break;
                case 34:
                    System.out.println("系统管理-删除房间");
                    deleteRoom();
                    break;
                case 35:
                    System.out.println("系统管理-删除学员");
                    deleteStudent();
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非法输入");
            String errorInput = scanner.nextLine();
            log.info(SystemEnum.ERROR_INPUT+errorInput);
        }
    }

    //11.房间录入
    public void addARoom(){
        System.out.println("输入房间号码：");
        Integer id = scanner.nextInt();
        System.out.println("输入性别：true-男 false-女");
        boolean sex = scanner.nextBoolean();
        System.out.println("输入可住人数：");
        int count = scanner.nextInt();

        Room room = new Room(id,sex?"男":"女",count,null,count,null);
        roomService.insert(room);
    }
    //12.房间修改
    public void alterRoom(){
        System.out.println("输入房间号码：");
        Integer id = scanner.nextInt();
        System.out.println("输入要修改的字段名：");
        String fieldName = scanner.next();
        System.out.println("输入要修改的字段值：");
        String fieldValue = scanner.next();

        Room room = roomService.findById(id);
        switch (fieldName){
            case "dormSex":
                if("男".equals(fieldValue)){
                    room.setSex("男");
                }else room.setSex("女");
                roomService.update(room);
                break;
            case "dormSize":
                room.setSize(Integer.valueOf(fieldValue));
                //////////////////////////////////////////////////////////////bed
                roomService.updateSize(room);
                break;
            case "dormCash":
                room.setCash(new BigDecimal(Double.valueOf(fieldValue)));
                roomService.update(room);
                break;
        }
    }
    //21.添加班级
    public void addClass(){
        System.out.println("输入添加的班级id：");
        Integer id = scanner.nextInt();
        System.out.println("输入添加的班级name：");
        String name = scanner.next();

        Classroom classroom = new Classroom(id,name);
        classroomService.insert(classroom);
    }
    //22.修改班级
    public void alterClass(){
        System.out.println("输入添加的班级id：");
        Integer id = scanner.nextInt();
        System.out.println("输入修改的班级name：");
        String name = scanner.next();
        Classroom classroom = new Classroom(id,name);
        classroomService.update(classroom);
    }
    //31.添加登陆用户
    public void addAdmin(){
        System.out.println("输入添加的管理员id：");
        Integer id = scanner.nextInt();
        System.out.println("输入密码：");
        String pw = scanner.next();
        System.out.println("输入姓名：");
        String name = scanner.next();
        System.out.println("输入电话号码：");
        String tel = scanner.next();
        System.out.println("是否为超管？ true-是 false-否");
        boolean isSuper = scanner.nextBoolean();
        Admin admin = new Admin(id,pw,name,tel,isSuper);
        adminService.insert(admin);
    }
    //32.管理人员删除
    public void deleteAdmin(){
        System.out.println("输入要删除的管理员id：");
        Integer id = scanner.nextInt();
        adminService.delete(id);
    }
    //33.人员信息修改
    public void alterAdmin(){
        System.out.println("输入修改的管理员id：");
        Integer id = scanner.nextInt();
        System.out.println("输入字段名：");
        String fieldName = scanner.next();
        System.out.println("输入字段值：");
        String fieldValue = scanner.next();
        Admin admin = adminService.findByAdminAcount(id);
        switch (fieldName){
            case "password":
                admin.setPassword(fieldValue);
                break;
            case "manaName":
                admin.setManaName(fieldValue);
                break;
            case "manaTel":
                admin.setManaTel(fieldValue);
                break;
            case "isSuper":
                admin.setSuper(!admin.isSuper());
                break;
            default:
                return;
        }
        adminService.update(admin);
    }
    //34.删除房间
    public void deleteRoom(){
        System.out.println("输入房间号码：");
        Integer id = scanner.nextInt();
        Room room = roomService.findById(id);
        roomService.delete(room);
    }
    //35.删除学员
    public void deleteStudent(){
        System.out.println("输入要删除的学生id：");
        Integer id = scanner.nextInt();
        studentService.delete(id);
    }

}
